package org.jeecgframework.web.material.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.material.entity.ProductProgressEntity;
import org.jeecgframework.web.material.service.ProductProgressServiceI;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


/**
 * @Description: 产品生产过程
 * @date 2015-08-19 09:52:16
 * @version V1.0
 * 
 */
@Scope("prototype")
@Controller
@RequestMapping("/productProgressController")
public class ProductProgressController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(ProductProgressController.class);

	@Autowired
	private ProductProgressServiceI productProgressService;
	@Autowired
	private SystemService systemService;
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "productProgress")
	public ModelAndView productProgress(HttpServletRequest request) {
		return new ModelAndView("material/productProgress/productProgressList");
	}

	/**
	 * 产品生产过程新增或更新页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAddOrUpdate")
	public ModelAndView goUpdate(ProductProgressEntity productProgress,
			HttpServletRequest req) {
		if (StringUtil.isNotEmpty(productProgress.getId())) {
			productProgress = productProgressService.getEntity(
					ProductProgressEntity.class, productProgress.getId());
			req.setAttribute("productProgressPage", productProgress);
		}
		return new ModelAndView("material/productProgress/productProgress");
	}

	/**
	 * 查询不同的product或ptype
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(ProductProgressEntity productProgress,
			HttpServletRequest request, HttpServletResponse response,
			DataGrid dataGrid) {
		
		try {
			List list = productProgressService.queryMain(productProgress);
			dataGrid.setResults(list);
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		
		TagUtil.datagrid(response, dataGrid);
		
	}
	
	/**
	 * 查询product或ptype下的生成过程
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagridProgress")
	public void datagridProgress(ProductProgressEntity productProgress,
			HttpServletRequest request, HttpServletResponse response,
			DataGrid dataGrid) {
		
		try {
			
			List list = productProgressService.queryProgress(productProgress);
			dataGrid.setResults(list);
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		
		TagUtil.datagrid(response, dataGrid);
		
	}

	/**
	 * 添加或更新产品生产过程
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAddOrUpdate")
	@ResponseBody
	public AjaxJson doAdd(ProductProgressEntity productProgress,
			HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isEmpty(productProgress.getId())) {
			message = "产品生产过程添加成功";
			try {
				productProgressService.addMain(productProgress);
				systemService.addLog(message, Globals.Log_Type_INSERT,
						Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "产品生产过程添加失败";
				throw new BusinessException(e.getMessage());
			}
			j.setMsg(message);
		} else {
			message = "产品生产过程更新成功";
			
			try {
				productProgressService.updateMain(productProgress);
				systemService.addLog(message, Globals.Log_Type_UPDATE,
						Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "产品生产过程更新失败";
				throw new BusinessException(e.getMessage());
			}
			j.setMsg(message);
		}

		return j;
	}

	/**
	 * 删除产品生产过程
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(ProductProgressEntity productProgress,
			HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		
		message = "产品生产过程删除成功";
		try {
			productProgressService.deleteMain(productProgress);
			systemService.addLog(message, Globals.Log_Type_DEL,
					Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "产品生产过程删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 批量删除产品生产过程
	 * 
	 * @return
	 */
	@RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "产品生产过程删除成功";
		try {
			for (String id : ids.split(",")) {
				ProductProgressEntity productProgress = systemService
						.getEntity(ProductProgressEntity.class, id);
				productProgressService.deleteMain(productProgress);
				systemService.addLog(message, Globals.Log_Type_DEL,
						Globals.Log_Leavel_INFO);
			}
		} catch (Exception e) {
			e.printStackTrace();
			message = "产品生产过程删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

}
